import {Action, Module, VuexModule} from 'vuex-class-modules'
import ComUserRO from "@/plugins/uniuser/model/ComUserRO";
import TokenUtil from "@/plugins/uniuser/utils/TokenUtil";
import UserAPI from "@/plugins/uniuser/api/UserAPI";
import {userModule} from "@/plugins/uniuser/store/index";
import ComLoginRO from "@/plugins/uniuser/model/ComLoginRO";

//用来存储当前用户的一些信息
@Module({generateMutationSetters: true})
export default class ComUserModule extends VuexModule {
    user: ComUserRO = null

    removeUser() {
        this.user = null
        TokenUtil.remove()
    }

    setUser(user: ComUserRO) {
        this.user = user
    }

    get hasUser(): boolean {
        return !!this.user
    }

    loginAfterSetUserAndToken(loginRO: ComLoginRO<ComUserRO>) {
        TokenUtil.set(loginRO.token)
        this.setUser(loginRO.user)
    }

    /**
     * 调用后台仅获取user信息
     */
    @Action
    getMineUserAction() {
        if (TokenUtil.get()) {
            return UserAPI.getMineUserAPI().then((res: any) => {
                userModule.setUser(res.data)
            })
        }
    }
}
